home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / flood.c < prev    next >
C/C++ Source or Header  |  1986-01-07  |  12KB  |  380 lines

  1.  
  2. /* examples of flood fills  -  v 1.1 ? */
  3.  
  4. #include "exec/types.h"
  5. #include "exec/nodes.h" 
  6. #include "exec/lists.h"
  7. #include "exec/interrupts.h"
  8. #include "exec/memory.h"
  9. #include "exec/ports.h"
  10. #include "exec/tasks.h"
  11. #include "exec/libraries.h"
  12. #include "exec/devices.h"
  13. #include "exec/io.h"
  14. #include "exec/execname.h"
  15. #include "exec/execbase.h"
  16. #include "devices/keymap.h"
  17. #include "graphics/gfx.h"
  18. #include "graphics/copper.h"
  19. #include "graphics/gels.h"
  20. #include "graphics/gfxbase.h"
  21. #include "graphics/gfxmacros.h"
  22. #include "graphics/regions.h"
  23. #include "hardware/blit.h"
  24. #include "intuition/intuition.h"
  25. #include "intuition/intuitionbase.h"
  26.  
  27. #define INTUITION_REV 1
  28. #define GRAPHICS_REV 1
  29.  
  30. #define SBMWIDTH 320   /* Screen BitMap Constants */
  31. #define SBMHEIGHT 200
  32. #define SBMDEPTH 4
  33.  
  34. #define WBMWIDTH 128   /* Window BitMap Constants */
  35. #define WBMHEIGHT 128
  36. #define WBMDEPTH 4
  37.  
  38. struct IntuitionBase *IntuitionBase = 0;
  39. struct GfxBase *GfxBase = 0;
  40.  
  41. struct IntuiMessage *MyIntuiMessage = 0;
  42.  
  43. struct TextAttr TestFont = { /* Needed for opening screen */
  44.    "topaz.font", 
  45.    TOPAZ_EIGHTY, 0, 0
  46. };
  47.  
  48. struct Screen *screen; /* These are for Custom screen */
  49. struct ViewPort *vp1;
  50.  
  51. struct Window *wNormal = 0; /* Two Windows */
  52. struct Window *wSuper = 0;
  53. struct BitMap MyBitMap; /* wSuper is a super bitmap, this is it */
  54.  
  55. UWORD areabuffer[250], areabuffer2[250];   /* These are for Fills */
  56. struct TmpRas myTmpRas, myTmpRas2;
  57. struct AreaInfo myAreaInfo, myAreaInfo2;
  58. PLANEPTR myplane, myplane2;
  59.  
  60. WORDBITS areapat1[] = {         /* These are for Fills */
  61.    0xCCCC, 
  62.    0xCCCC, 
  63.    0xCCCC, 
  64.    0xCCCC, 
  65.    0xCCCC, 
  66.    0xCCCC, 
  67.    0xCCCC, 
  68.    0xCCCC 
  69. };
  70.  
  71. WORDBITS areapat2[] = {         /* These are for Fills */
  72.    0xAAAA, 
  73.    0xAAAA, 
  74.    0xAAAA, 
  75.    0xAAAA, 
  76.    0xAAAA, 
  77.    0xAAAA, 
  78.    0xAAAA, 
  79.    0xAAAA 
  80. };
  81.  
  82. struct NewScreen ns = {      /* These are for my custom screen */
  83.    0, 0,                     /* start position */
  84.    SBMWIDTH, SBMHEIGHT, SBMDEPTH,   /* width, height, depth */
  85.    7, 3,                     /* detail pen, block pen */
  86.    NULL,                     /* viewing mode */
  87.    CUSTOMSCREEN,             /* screen type */
  88.    &TestFont,                /* font to use */
  89.    "BOBS",                   /* default title for screen */
  90.    NULL                      /* pointer to additional gadgets */
  91. };
  92.  
  93. struct NewWindow nwNormal = {
  94.    00, 11,                   /* start position (left, top) */
  95.    WBMWIDTH, WBMHEIGHT,      /* width, height */
  96.    -1, -1,                   /* detail pen, block pen */
  97.    CLOSEWINDOW,              /* IDCMP flags */
  98.    WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE
  99.    | SMART_REFRESH | GIMMEZEROZERO | ACTIVATE,   /* window flags */
  100.    NULL,                     /* pointer to first user gadget */
  101.    NULL,                     /* pointer to first checkmarkw */
  102.    "Smart",                  /* window title */
  103.    NULL,                     /* pointer to screen  (filled later) */
  104.    NULL,                     /* pointer to superbitmap */
  105.    40, 20, WBMWIDTH, WBMHEIGHT, /* ignored, not a sized window */
  106.    CUSTOMSCREEN              /* type of screen for this window */
  107. };
  108.  
  109. struct NewWindow nwSuper = {
  110.    130, 11,                  /* start position */
  111.    WBMWIDTH, WBMHEIGHT,      /* width, height */
  112.    -1, -1,                   /* detail pen, block pen */
  113.    CLOSEWINDOW,              /* IDCMP flags */
  114.    WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE
  115.    | SUPER_BITMAP | GIMMEZEROZERO | ACTIVATE,   /* window flags */
  116.    NULL,                     /* pointer to first user gadget    */
  117.    NULL,                     /* pointer to first checkmark */
  118.    "Super",                  /* title */
  119.    NULL,                     /* pointer to screen  (filled later) */
  120.    NULL,                     /* pointer to superbitmap (filled later) */
  121.    40, 20, WBMWIDTH, WBMHEIGHT, /* ignored, not a sized window */
  122.    CUSTOMSCREEN              /* type of screen for this window */
  123. };
  124.  
  125. main()
  126. {
  127.  
  128.    WORD i;
  129.  
  130. /*** Open libraries that will be needed **********************/
  131.  
  132.    if ((IntuitionBase = (struct IntuitionBase *)
  133.       OpenLibrary("intuition.library", INTUITION_REV)) == 0)
  134.    {
  135.       MyCleanup();
  136.       Exit(FALSE);
  137.    }
  138.  
  139.    if ((GfxBase = (struct GfxBase *)
  140.     OpenLibrary("graphics.library", GRAPHICS_REV)) == 0) {
  141.       MyCleanup();
  142.       Exit(FALSE);
  143.    }
  144.  
  145.    /*** Open My Very Own Screen ******************************/
  146.  
  147.    screen = (struct Screen *)OpenScreen(&ns);
  148.  
  149.    /*** Open a regular window ********************************/
  150.  
  151.    nwNormal.Screen = screen;
  152.    wNormal = (struct Window *)OpenWindow(&nwNormal);
  153.  
  154.    /*** Open a SuperBitMap window ****************************/
  155.  
  156.    /*** First Initialize, Then fill fill in missing pointers ***/ 
  157.    InitBitMap(&MyBitMap, WBMDEPTH, WBMWIDTH, WBMHEIGHT);
  158.    for(i=0;i<WBMDEPTH;i++) {
  159.       if ((MyBitMap.Planes[i] = (PLANEPTR)
  160.        AllocRaster(WBMWIDTH, WBMHEIGHT)) == 0) {  
  161.          MyCleanup();
  162.          Exit(FALSE);
  163.       }
  164.       BltClear(MyBitMap.Planes[i], (WBMWIDTH/8)*WBMHEIGHT, 1);
  165.    }
  166.  
  167.    /* Set up those pointers that couldn't be initialized staticly */
  168.    nwSuper.Screen = screen;
  169.    nwSuper.BitMap = &MyBitMap;
  170.  
  171.    /*** Call OpenWindow, Now that everything is set up *******/
  172.    if ((wSuper = (struct Window *)
  173.       OpenWindow(&nwSuper)) == NULL)
  174.    {
  175.       MyCleanup();
  176.       Exit(FALSE);
  177.    }
  178.  
  179.    /*** Set Normals' colors **********************************/
  180.  
  181.    vp1 = &wNormal->WScreen->ViewPort;
  182.    SetRGB4(vp1, 00, 00, 00, 00);   /* Black             */
  183.    SetRGB4(vp1, 01, 15, 00, 00);   /* Red               */
  184.    SetRGB4(vp1, 02, 00, 15, 00);   /* Green             */
  185.    SetRGB4(vp1, 03, 00, 00, 15);   /* Deep Blue         */
  186.    SetRGB4(vp1, 04, 15, 15, 00);   /* Yellow            */
  187.    SetRGB4(vp1, 05, 15, 00, 15);   /* Magenta           */
  188.    SetRGB4(vp1, 06, 08, 15, 15);   /* Cyan              */
  189.    SetRGB4(vp1, 07, 15, 11, 00);   /* Orange            */
  190.    SetRGB4(vp1, 08, 05, 13, 00);   /* Lime Green        */
  191.    SetRGB4(vp1, 09, 14, 03, 00);   /* Fire Engine Red   */
  192.    SetRGB4(vp1, 10, 15, 02, 14);   /* Violet            */
  193.    SetRGB4(vp1, 11, 15, 13, 11);   /* Tan               */
  194.    SetRGB4(vp1, 12, 12, 09, 08);   /* Brown             */
  195.    SetRGB4(vp1, 13, 11, 11, 11);   /* Grey              */
  196.    SetRGB4(vp1, 14, 07, 13, 15);   /* Sky Blue          */
  197.    SetRGB4(vp1, 15, 15, 15, 15);   /* White             */
  198.  
  199.    /*** Initialize regular window for Fills ******************/
  200.  
  201.    InitArea(&myAreaInfo, &areabuffer[0], 100);
  202.    if ((myplane = (PLANEPTR)
  203.     AllocRaster(WBMWIDTH, WBMHEIGHT)) == NULL) {
  204.       MyCleanup();
  205.       Exit(FALSE);
  206.    }
  207.    wNormal->RPort->TmpRas = (struct TmpRas *)
  208.     InitTmpRas(&myTmpRas, myplane, RASSIZE(WBMWIDTH, WBMHEIGHT));
  209.    wNormal->RPort->AreaInfo = &myAreaInfo;
  210.  
  211.    /*** Initialize SuperBitMap window for Fills **************/
  212.  
  213.    InitArea(&myAreaInfo2, &areabuffer2[0], 100);
  214.    if ((myplane2 = (PLANEPTR)
  215.     AllocRaster(WBMWIDTH, WBMHEIGHT)) == NULL) { 
  216.       MyCleanup();
  217.       Exit(FALSE);
  218.    }
  219.    wSuper->RPort->TmpRas = (struct TmpRas *)
  220.       InitTmpRas(&myTmpRas2, myplane2, RASSIZE(WBMWIDTH, WBMHEIGHT));
  221.    wSuper->RPort->AreaInfo = &myAreaInfo2;
  222.  
  223.    /*** Draw and fill in both windows ************************/
  224.  
  225.    DrawSquare(wNormal, 2,  5,  5, 100, 100);
  226.    DrawSquare(wNormal, 2, 10, 10,  35,  35);
  227.    DrawSquare(wNormal, 3, 40, 10,  65,  35);
  228.    DrawSquare(wNormal, 2, 70, 10,  95,  35);
  229.    DrawSquare(wNormal, 3, 10, 40,  35,  65);
  230.    DrawSquare(wNormal, 4, 40, 40,  65,  65);
  231.    DrawSquare(wNormal, 3, 70, 40,  95,  65);
  232.    DrawSquare(wNormal, 2, 10, 70,  35,  95);
  233.    DrawSquare(wNormal, 3, 40, 70,  65,  95);
  234.    DrawSquare(wNormal, 2, 70, 70,  95,  95);
  235.  
  236.    SetAfPt(wNormal->RPort, areapat1, 3);
  237.    SetOPen(wNormal->RPort, 2);
  238.  
  239.    SetAPen(wNormal->RPort, 4);
  240.    Flood(wNormal->RPort, 1, 11, 11);
  241.    SetAPen(wNormal->RPort, 5);
  242.    Flood(wNormal->RPort, 1, 41, 11);
  243.    SetAPen(wNormal->RPort, 6);
  244.    Flood(wNormal->RPort, 1, 71, 11);
  245.    SetAPen(wNormal->RPort, 7);
  246.    Flood(wNormal->RPort, 1, 11, 41);
  247.    SetAPen(wNormal->RPort, 8);
  248.    Flood(wNormal->RPort, 1, 41, 41);
  249.    SetAPen(wNormal->RPort, 9);
  250.    Flood(wNormal->RPort, 1, 71, 41);
  251.    SetAPen(wNormal->RPort, 10);
  252.    Flood(wNormal->RPort, 1, 11, 71);
  253.    SetAPen(wNormal->RPort, 11);
  254.    Flood(wNormal->RPort, 1, 41, 71);
  255.    SetAPen(wNormal->RPort, 12);
  256.    Flood(wNormal->RPort, 1, 71, 71);
  257.  
  258.    SetAfPt(wNormal->RPort, areapat2, 3);
  259.    SetAPen(wNormal->RPort, 13);
  260.    Flood(wNormal->RPort, 0, 6, 6);
  261.  
  262.    DrawSquare(wSuper, 3,  5,  5, 100, 100);
  263.    DrawSquare(wSuper, 2, 10, 10,  35,  35);
  264.    DrawSquare(wSuper, 3, 40, 10,  65,  35);
  265.    DrawSquare(wSuper, 2, 70, 10,  95,  35);
  266.    DrawSquare(wSuper, 3, 10, 40,  35,  65);
  267.    DrawSquare(wSuper, 1, 40, 40,  65,  65);
  268.    DrawSquare(wSuper, 3, 70, 40,  95,  65);
  269.    DrawSquare(wSuper, 2, 10, 70,  35,  95);
  270.    DrawSquare(wSuper, 3, 40, 70,  65,  95);
  271.    DrawSquare(wSuper, 2, 70, 70,  95,  95);
  272.  
  273.    SetAfPt(wSuper->RPort, areapat1, 3);
  274.    SetOPen(wSuper->RPort, 3);
  275.  
  276.    SetAPen(wSuper->RPort, 4);
  277.    Flood(wSuper->RPort, 1, 11, 11);
  278.    SetAPen(wSuper->RPort, 5);
  279.    Flood(wSuper->RPort, 1, 41, 11);
  280.    SetAPen(wSuper->RPort, 6);
  281.    Flood(wSuper->RPort, 1, 71, 11);
  282.    SetAPen(wSuper->RPort, 7);
  283.    Flood(wSuper->RPort, 1, 11, 41);
  284.    SetAPen(wSuper->RPort, 8);
  285.    Flood(wSuper->RPort, 1, 41, 41);
  286.    SetAPen(wSuper->RPort, 9);
  287.    Flood(wSuper->RPort, 1, 71, 41);
  288.    SetAPen(wSuper->RPort, 10);
  289.    Flood(wSuper->RPort, 1, 11, 71);
  290.    SetAPen(wSuper->RPort, 11);
  291.    Flood(wSuper->RPort, 1, 41, 71);
  292.    SetAPen(wSuper->RPort, 12);
  293.    Flood(wSuper->RPort, 1, 71, 71);
  294.  
  295.    SetAfPt(wSuper->RPort, areapat2, 3);
  296.    SetAPen(wSuper->RPort, 13);
  297.    Flood(wSuper->RPort, 0, 6, 6);
  298.  
  299.    for (;;)
  300.    {
  301.       Wait ((1 << wNormal->UserPort->mp_SigBit) /* wait on either */
  302.          | (1 << wSuper->UserPort->mp_SigBit));
  303.       while(MyIntuiMessage = (struct IntuiMessage *)
  304.       GetMsg(wNormal->UserPort))
  305.       switch (MyIntuiMessage->Class) {
  306.          case CLOSEWINDOW:
  307.             ReplyMsg(MyIntuiMessage);
  308.             MyCleanup();
  309.             Exit(TRUE);
  310.          default:
  311.             ReplyMsg(MyIntuiMessage);
  312.             break;
  313.       }
  314.       while(MyIntuiMessage = (struct IntuiMessage *)
  315.       GetMsg(wSuper->UserPort))
  316.       switch (MyIntuiMessage->Class) {
  317.          case CLOSEWINDOW:
  318.             ReplyMsg(MyIntuiMessage);
  319.             MyCleanup();
  320.             Exit(TRUE);
  321.             break;
  322.          default:
  323.             ReplyMsg(MyIntuiMessage);
  324.             break;
  325.       } /* switch */
  326.    } /* for */
  327. } /* end of Main */
  328.  
  329. MyCleanup_Normal()
  330. {
  331.    while(MyIntuiMessage = (struct IntuiMessage *)GetMsg(wNormal->UserPort))
  332.       ReplyMsg(MyIntuiMessage);
  333.    if (wNormal != 0)
  334.    CloseWindow(wNormal);
  335.    if (myplane != 0)
  336.    FreeRaster(myplane, WBMWIDTH, WBMHEIGHT);
  337. }
  338.  
  339. MyCleanup_Super()
  340. {
  341.    WORD i;
  342.    while(MyIntuiMessage = (struct IntuiMessage *)GetMsg(wSuper->UserPort))
  343.       ReplyMsg(MyIntuiMessage);
  344.    if (wSuper != 0)
  345.    CloseWindow(wSuper);
  346.    if (myplane2 != 0)
  347.    FreeRaster(myplane2, WBMWIDTH, WBMHEIGHT);
  348.    for (i=0; i<WBMDEPTH; i++)
  349.    {
  350.       if (MyBitMap.Planes[i] != 0)
  351.       FreeRaster(MyBitMap.Planes[i], WBMWIDTH, WBMHEIGHT);
  352.    }
  353. }
  354.  
  355. MyCleanup()
  356. {
  357.    MyCleanup_Normal();
  358.    MyCleanup_Super();
  359.    if (screen != 0)
  360.    CloseScreen(screen);
  361.    if (GfxBase != 0)
  362.    CloseLibrary(GfxBase);
  363.    if (IntuitionBase != 0)
  364.    CloseLibrary(IntuitionBase);
  365. }
  366.  
  367. DrawSquare(window, pen, x, y, x1, y1)
  368. struct Window *window;
  369. WORD pen, x, y, x1, y1; 
  370. {
  371.    SetAPen(window->RPort, pen);
  372.    SetDrMd(window->RPort, JAM1);
  373.    Move(window->RPort, x,   y);
  374.    Draw(window->RPort, x,  y1);
  375.    Draw(window->RPort, x1, y1);
  376.    Draw(window->RPort, x1,  y);
  377.    Draw(window->RPort, x,   y);
  378. }
  379.  
  380.